package com.lilong.jwt.service;


import com.lilong.jwt.entity.user.User;
import com.lilong.jwt.repository.user.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by sang on 2017/1/10.
 */
public class CustomUserService implements UserDetailsService {
    @Resource
    private UserRepository userRepository;
    @Override
    @Transactional
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(s);
        if (user != null) {
            user.getRoles().forEach(p->p.getPermissions().size());
            return user;
        } else {
            throw new UsernameNotFoundException("用户名不存在");
        }
    }
}
